<template>
	<view>
		<view class="tabWrap">
			<view class="tabWrapItem">
				<view class="tabItem"
					:class="[orderStaIndex === index ? 'active' : '']"
					v-for="(item,index) in orderStaArr"
					:key="index"
					@tap="switchStatus(index)"
				>{{item.name}}<view class="lineView"></view></view>
			</view>
			<view class="tabWrapItem">
				<view class="tabItem sm"
					:class="[subIndex === index ? 'active' : '']"
					v-for="(item,index) in subList"
					:key="index"
					@tap="switchSubject(index,item)"
				>{{item.subjectName}}</view>
			</view>
		</view>
		<view class="errList padd">
			<template v-if="isHasDataFlag">
				<view class="listItem"
					v-for="(item,index) in errBookList"
					:key="index"
				> 
					<view class="queType">{{item.questionType}}</view>
					<view class="queTit">
						<rich-text :nodes="formatImgRichText(item.subjectName)"></rich-text>
					</view>
					<view class="queBot spaceBet">
						<view class="errTime">{{item.addTime}}</view>
						<view class="botBtn hasFlex">
							<view class="dzBtn btn" @tap="goQuestion(item.id)" v-if="item.comTag === 1">去订正</view>
							<view class="viewBtn btn" @tap="goQueDet(item.id)">查看详情</view>
						</view>
					</view>
				</view>
				<loading-view :loading="loading"></loading-view>
			</template>
			<template v-else-if="isHasDataFlag == false">
				<no-data></no-data>
			</template>
		</view>
	</view>
</template>

<script>
import { getErrBookList,getSelfSubject } from '@/api/zhuxue.js'
	export default {
		data(){
			return{
				orderStaArr:[
					{id:0,name:'全部'},
					{id:1,name:'未订正'},
					{id:2,name:'已订正'},
				],
				orderStaIndex:0,
				subList:[],
				subIndex:0,
				subId:0,
				nowPage:1,
				loading:false,
				errBookList:[],
				isHasDataFlag:null,
				isRefreshFlag:false
			}
		},
		computed:{
			//设置img宽度
			formatImgRichText(){
				return function(content){
					if(content != undefined ){
						var reg = new RegExp("<img", "g"),regModule = new RegExp("Module/", "g");
						if(content.indexOf('Module/') != -1){
							content = content.replace(regModule, this.zhuXueServer + '/Module/');
							return content.replace(reg, '<img style="max-width:100%;height:auto;display:block"')
						}else{
							return content
						}
					}
				}
			},
		},
		onShow(){
			if(this.isRefreshFlag){
				this.initParams();
			}
		},
		onHide(){
			this.isRefreshFlag = false;
		},
		onPullDownRefresh : function(){
			this.initParams();
			uni.stopPullDownRefresh();
		},
		onReachBottom : function(){
			if( !this.loading ){
				this.loadErrBookList();
			}
		}, 
		onLoad(){
			this.getSelfSubjectList();
		},
		methods:{
			getSelfSubjectList(){
				let field = { account:uni.getStorageSync('token') };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				getSelfSubject({content:contentStr}).then(res=>{
					let tmpRes = res.data.datas;
					this.subList = tmpRes.subjectList;
					this.subId = this.subList[0].subjectId;
					this.loadErrBookList();
				}).catch(err=>{
					console.log(err)
				});
			},
			loadErrBookList(){
				let { nowPage,errBookList } = this;
				this.loading = true;
				var field = { userId:uni.getStorageSync('userId'),subId:this.subId,comTag:this.orderStaIndex,pageNo:this.nowPage,pageSize:this.limit };
				getErrBookList(field).then(res=>{
					if(res.data.datas.length === 0 && this.nowPage === 1){
						this.isHasDataFlag = false;
					}else{
						this.isHasDataFlag = true;
						nowPage += 1;
						errBookList.push( ...res.data.datas );
						this.errBookList = errBookList;
						this.nowPage = nowPage;
						this.loading = false;
					}
				}).catch(err=>{
					//console.log(err)
					this.loading = false;
					this.isHasDataFlag=false;
				});
			},
			switchStatus(index){
				this.orderStaIndex = index;
				this.initParams();
			},
			switchSubject(index,item){
				this.subIndex = index;
				this.subId = item.subjectId;
				this.initParams();
			},
			initParams(){
				this.nowPage = 1;
				this.loading = false;
				this.errBookList = [];
				this.loadErrBookList();
			},
			goQuestion(id){
				uni.navigateTo({
					url:'/pages/subIndex/errBook/errQue?loreErrId=' + id
				})
			},
			goQueDet(id){
				uni.navigateTo({
					url:'/pages/subIndex/errBook/errQueDet?loreErrId=' + id
				})
			}
		}
	}
</script>
<style>
	page{
		background: #f9f8f6;
	}
</style>
<style lang="scss" scoped>
	@import '@/static/scss/errBookList.scss';
</style>